import React,{Component} from 'react'
import {
    Form,
    Input,
    Select,
    Checkbox,
    Button,
    AutoComplete,
    message,
  } from 'antd';
  import { layout, tailLayout } from '../../const.js';

  import {updateadmin} from '../../apis/admin.js'
  const { Option } = Select;
  const AutoCompleteOption = AutoComplete.Option;


export default class Changeadmin extends Component {
    state = {
        // button点击后弹框
        loading: false,
    }
    onFinish = async (values) => {
        try {
            this.setState({loading:true});
            const data = await updateadmin(values);
            console.log("点击修改后从后端传过来的数据",data);
            this.setState({loading:false})
            message.success("账号修改成功");
            this.props.history.push('/home/adminlist') ;
        } catch (error) {


        }
    };

    onFinishFailed = (errorInfo) => {
        console.log('Failed:', errorInfo);
    };
    componentDidMount(options){
    }
    render() {
        const { loading ,} = this.state;
        const list =this.props.location.state;
        console.log(list);
            if(!list){
            this.props.history.push('/home/adminlist') ;
         }
        return (
            <div>
            <Form
                className=" "
                {...layout}
                name="basic"
                initialValues={{
                    adminname:list[0].adminname||'',
                    password:list[0].password||'',
                    state:list[0].state||'',
                    userGroup:list[0].userGroup|'',
                    remember: true,
                }}
                onFinish={this.onFinish}
                onFinishFailed={this.onFinishFailed}
                ref={instance => this.myform = instance}
            >
                <Form.Item
                    label={<div style={{ width: 100 }}>用户名</div>}
                    name="adminname"
                    rules={[
                        {
                            required: true,
                            message: 'Please input your username!',
                        },
                    ]}
                >
                    <Input   />
                </Form.Item>

                <Form.Item

                    label={<div style={{ width: 100 }}>密码</div>}
                    name="password"
                    rules={[
                        {
                            required: true,
                            message: 'Please input your password!',
                        },
                    ]}
                >
                    <Input.Password autoComplete="newPassword" />
                </Form.Item>

                <Form.Item
                    label={<div style={{ width: 100 }}>账号状态</div>}
                    name="state"
                    rules={[
                        {
                            required: true,
                            message: 'Please input your state!',
                        },
                    ]}
                >
                    <Select style={{ width: 120 }}  >
                        <Option value="生效中">生效中</Option>
                        <Option value="已注销">已注销</Option>
                    </Select>
                </Form.Item>

                <Form.Item
                    label={<div style={{ width: 100 }}>角色</div>}
                    name="userGroup"
                    rules={[
                        {
                            required: true,
                            message: 'Please input your username!',
                        },
                    ]}
                >
                    <Select style={{ width: 120 }}  >
                        <Option value="超级管理员">超级管理员</Option>
                        <Option value="普通管理员">普通管理员</Option>
                        <Option value="普通用户">普通用户</Option>
                    </Select>
                </Form.Item>
                <Form.Item  {...tailLayout} name="remember" valuePropName="checked">
                    <Checkbox>i agree</Checkbox>
                </Form.Item>

                <Form.Item {...tailLayout}>
                    <Button type="primary" htmlType="submit" loading={loading}>确认修改</Button>
                </Form.Item>
            </Form>
        </div>
        )
    }
}
